using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using sfcms.api;

/// <summary>
/// Summary description for DHtmlMenu
/// </summary>
public class DHtmlMenu
{
    public static void EnsureDHtmlMenuContent(string culture)
    {
        if (HttpContext.Current.Application["Menu " + culture] == null)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<ul id=\"sddm\">");

            foreach (folder f in folder.byparentid(0, folder.pageroot.type))
            {
                if (f.name == culture)
                {
                    int menuIdIndex = 0;

                    foreach (page def in page.byparentid(f.id))
                    {
                        sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", def.friendlyurl, def.displayname);
                    }
                    foreach (folder sf in folder.byparentid(f.id, 1))
                    {
                        menuIdIndex++;
                        sb.AppendFormat("<li><a href=\"#\" onmouseover=\"mopen('MENU_{0}')\" onmouseout=\"mclosetime()\">{1}</a>", menuIdIndex, sf.displayname);
                        page[] pages = page.byparentid(sf.id);
                        if (pages.Length > 0)
                        {
                            sb.AppendFormat("<div id=\"MENU_{0}\" onmouseover=\"mcancelclosetime()\" onmouseout=\"mclosetime()\">", menuIdIndex);
                            foreach (page p in pages)
                            {
                                sb.AppendFormat("<a href=\"{0}\">{1}</a>", p.friendlyurl, p.displayname);
                            }
                            sb.Append("</div>");
                        }
                        sb.AppendFormat("</li>");
                    }
                }
            }
            sb.Append("</ul><div style=\"clear:both\"></div>");

            HttpContext.Current.Application["Menu " + culture] = sb;
        }
    }
}
